<template>
    <el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible"
      :close-on-click-modal="false" draggable>
      <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
       <el-row :gutter="24">
    <el-col :span="12" class="mb20">
      <el-form-item label="参与组织名称" prop="partOrgName">
        <el-input v-model="form.partOrgName" placeholder="请输入参与组织名称"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="试题岗位名称" prop="partPostName">
        <el-input v-model="form.partPostName" placeholder="请输入试题岗位名称"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="个人岗位名称" prop="personalPostName">
        <el-input v-model="form.personalPostName" placeholder="请输入个人岗位名称"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="题型" prop="questionType">
        <el-input v-model="form.questionType" placeholder="请输入题型"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="题干" prop="stem">
        <el-input v-model="form.stem" placeholder="请输入题干"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="专业名称" prop="zymcName">
        <el-input v-model="form.zymcName" placeholder="请输入专业名称"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="关键点" prop="keyPoint">
        <el-input v-model="form.keyPoint" placeholder="请输入关键点"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="资料解析" prop="readouts">
        <el-input v-model="form.readouts" placeholder="请输入资料解析"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="答案" prop="answer">
        <el-input v-model="form.answer" placeholder="请输入答案"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="难易程度" prop="difficulty">
        <el-input v-model="form.difficulty" placeholder="请输入难易程度"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="关联资料" prop="relatedInfo">
        <el-input v-model="form.relatedInfo" placeholder="请输入关联资料"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="选项A" prop="optionA">
        <el-input v-model="form.optionA" placeholder="请输入选项A"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="选项B" prop="optionB">
        <el-input v-model="form.optionB" placeholder="请输入选项B"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="选项C" prop="optionC">
        <el-input v-model="form.optionC" placeholder="请输入选项C"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="选项D" prop="optionD">
        <el-input v-model="form.optionD" placeholder="请输入选项D"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="工号" prop="userId">
        <el-input v-model="form.userId" placeholder="请输入工号"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label="人员姓名" prop="userName">
        <el-input v-model="form.userName" placeholder="请输入人员姓名"/>
      </el-form-item>
      </el-col>

			</el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
        </span>
      </template>
    </el-dialog>
</template>

<script setup lang="ts" name="TkglWrongQuestionDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj } from '/@/api/train/tkglWrongQuestion'
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
// 定义字典
const { tkgl_topic_type } = useDict('tkgl_topic_type')

// 提交表单数据
const form = reactive({
		id:'',
	  partOrgName: '',
	  partPostName: '',
	  personalPostName: '',
	  questionType: '',
	  stem: '',
	  zymcName: '',
	  keyPoint: '',
	  readouts: '',
	  answer: '',
	  difficulty: '',
	  relatedInfo: '',
	  optionA: '',
	  optionB: '',
	  optionC: '',
	  optionD: '',
	  userId: '',
	  userName: '',
});

// 定义校验规则
const dataRules = ref({
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''

  // 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

  // 获取tkglWrongQuestion信息
  if (id) {
    form.id = id
    gettkglWrongQuestionData(id)
  }
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
    loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
    loading.value = false;
  }
};


// 初始化表单数据
const gettkglWrongQuestionData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>
